package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduCourseDescription;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.frontvo.CourseFrontVo;
import com.atguigu.eduservice.entity.frontvo.CourseWebVo;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.mapper.EduCourseMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseDescriptionService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exception.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-08-30
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    //注入课程描述表service对象
    @Autowired
    private EduCourseDescriptionService courseDescriptionService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private EduVideoService videoService;



    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        //1.向课程表添加课程基本信息
        //将CourseInfoVo 对象转换为 原始对应数据库的对象
        EduCourse eduCourse = new EduCourse();
        //先把courseInfoVo 对象的属性get出来，然后再用原始对象set进去设置属性值
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int insert = baseMapper.insert(eduCourse);
        //判断
        if (insert == 0) {
            throw new GuliException(201,"添加课程信息失败");
        }

        //获取添加后的课程id
        String cid = eduCourse.getId();

        //2.向课程描述表中添加课程简介数据
        EduCourseDescription courseDescription = new EduCourseDescription();
        //把courseInfoVo 对象里的描述信息赋值到描述表对象中
        courseDescription.setDescription(courseInfoVo.getDescription());
        courseDescription.setId(cid); //和课程表的id相关联，使她们可以互相关联，查询的时候关联在一起
        courseDescriptionService.save(courseDescription); //调用保存方法
        return cid; //返回课程id给前端使用
    }

    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        //1.先查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        //把对应数据库表字段的实体类对象赋值到封装实体类vo对象当中
        BeanUtils.copyProperties(eduCourse,courseInfoVo);
        //2.查询描述课程信息的表
        EduCourseDescription courseDescription = courseDescriptionService.getById(courseId);
        //获取描述信息的内容，存放到vo实体类对象中
        courseInfoVo.setDescription(courseDescription.getDescription());
        //返回封装后的实体类对象
        return courseInfoVo;
    }

    @Transactional
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        //1.修改课程信息
        EduCourse eduCourse = new EduCourse();
        //把vo对象赋值到原始实体类对象当中
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int update = baseMapper.updateById(eduCourse);
        if (update == 0) {
            throw new GuliException(201,"修改课程信息失败");
        }

        //2.修改描述表
        EduCourseDescription description = new EduCourseDescription();
        //把vo中的数据赋值到EduCourseDescription 对象当中
        description.setId(courseInfoVo.getId());
        description.setDescription(courseInfoVo.getDescription());
        //调用修改方法
        boolean desc = courseDescriptionService.updateById(description);

        if (!desc) {
            throw new GuliException(201,"修改课程描述失败");
        }
    }

    @Override
    public CoursePublishVo publishCourseInfo(String id) {
        return baseMapper.getPublishCourseInfo(id);
    }

    //删除课程方法
    @Override
    @Transactional
    public void removeCourse(String courseId) {
        //1.根据课程id删除课程小节
        videoService.removeVideoByCourseId(courseId);

        //2.根据课程id删除章节
        chapterService.removeChapterByCourseId(courseId);

        //3.根据课程id删除描述
        courseDescriptionService.removeById(courseId);

        //4.根据课程id删除课程
        int result = baseMapper.deleteById(courseId);
        if (result == 0) {
            throw new GuliException(201,"删除失败");
        }
    }

    @Override
    public Map<String, Object> getCourseFrontList(Page<EduCourse> coursePage, CourseFrontVo courseFrontVo) {
        //根据讲师id查询课程
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        //判断条件值是否为空，不为空再执行条件
        if (!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())) {
            wrapper.eq("subject_parent_id", courseFrontVo.getSubjectParentId());
        }
        if (!StringUtils.isEmpty(courseFrontVo.getSubjectId())) {
            wrapper.eq("subject_id", courseFrontVo.getSubjectId());
        }
        if (!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())) { //关注度排序
            wrapper.orderByDesc("buy_count");
        }
        if (!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())) {//根据最新时间排序
            wrapper.orderByDesc("gmt_create");
        }
        if (!StringUtils.isEmpty(courseFrontVo.getPriceSort())) {
            wrapper.orderByDesc("price");
        }
        baseMapper.selectPage(coursePage,wrapper);

        List<EduCourse> records = coursePage.getRecords();//获取总记录

        long current = coursePage.getCurrent();
        long pages = coursePage.getPages();
        long size = coursePage.getSize();
        long total = coursePage.getTotal();
        boolean hasNext = coursePage.hasNext();
        boolean hasPrevious = coursePage.hasPrevious();
        Map<String, Object> map = new HashMap<>();
        //把数据放入map集合
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        return map;
    }

    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {

        return baseMapper.getBaseCourseInfo(courseId);
    }
}
